<?php
class AdsActionsController extends LilAdsAppController {
	var $name = 'AdsActions';
	var $uses = array('LilAds.AdsAction', 'LilAds.AdsCampaign');
	var $helpers = array('Time');
/**
 * isAuthorized method
 *
 * @access public
 * @return bool
 */
	function isAuthorized() {
		if (!parent::isAuthorized()) return false;
		
		// plugins can also change authorization
		$c = $this->callPluginHandlers('isAuthorizedLilAds', array('return' => true, 'continue' => true));
		if (!isset($c['return']) || $c['return'] == false) return false;
		
		// plugin can determine if it wants to end authorization check
		if (!isset($c['continue']) || $c['continue'] == true) {
			return $this->isAdmin();
		}
		return true;
	}
	
	function admin_daily() {
		$params = $this->params['named'];
		
		if (empty($params['start'])) {
			$params['start'] = strftime('%Y-%m-%d', mktime(0, 0, 0, date("m"), 1, date("Y"))); 
		}
		
		if (empty($params['end'])) {
			$params['end'] = strftime('%Y-%m-%d', mktime(0, 0, 0, date("m")+1, 0, date("Y"))); 
		}
		
		if (empty($params['campaign_id']) || !is_numeric($params['campaign_id'])) {
			$this->redirect(array('controller'=>'ads_campaigns', 'action'=>'stats'));
		}
		
		$this->set('campaign', $this->AdsCampaign->read(null, $params['campaign_id']));
		$this->set('stats', $this->AdsAction->getStats($params));
		$this->set('params', $params);
	}

	function admin_hourly() {
		$params = $this->params['named'];
		
		if (empty($params['date'])) {
			$params['date'] = strftime('%Y-%m-%d'); 
		}
		$params['start'] = $params['date'];
		$params['end'] = strftime('%Y-%m-%d', strtotime($params['start'])+24*60*60);
		
		if (empty($params['campaign_id']) || !is_numeric($params['campaign_id'])) {
			$this->redirect(array('controller'=>'campaings', 'action'=>'stats'));
		}
		
		$this->set('campaign', $this->AdsCampaign->read(null, $params['campaign_id']));
		$this->set('stats', $this->AdsAction->getStats($params, 13));
		
		// clear so these params doesn't get into url
		unset($params['start']); unset($params['end']);
		$this->set('params', $params);
	}
}
?>